package Demo15;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo15 {
    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);

        //生产者线程
        Thread producer = new Thread(() -> {
            int count = 0;
            try {
                while (true) {
                    queue.put("" + count);
                    System.out.println("生产了一个元素: " + count);
                    count++;
                    //设置生产速度
                    //Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        //消费者线程
        Thread consumer = new Thread(() -> {
            try {
                while (true) {
                    String elem = queue.take();
                    System.out.println("消费了一个元素: " + elem);
                    //设置消费速度
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        producer.start();
        consumer.start();
        producer.join();
        consumer.join();
    }
}